package com.turnengine.client.global.admin.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import java.io.IOException;

/**
 * The Send Email Data Serializer.
 */
public class SendEmailDataSerializer extends AbstractCommandDataSerializer<Boolean> implements ISendEmailDataSerializer {

	/**
	 * Creates a new Send Email Data Serializer.
	 */
	public SendEmailDataSerializer() {
		super(SendEmail.COMMAND_ID);
	}

	@Override
	public Boolean readReturnValue(IDataReader reader) throws IOException {
		Boolean value = reader.readBooleanObject(true);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, Boolean value) throws IOException {
		writer.writeBooleanObject(value, true);
	}

	@Override
	public IExecutableCommand<Boolean> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		String sender = reader.readString(true);
		String recipient = reader.readString(true);
		String subject = reader.readString(true);
		String body = reader.readString(true);

		// Create object
		SendEmail object = new SendEmail();
		object.setLoginId(loginId);
		object.setSender(sender);
		object.setRecipient(recipient);
		object.setSubject(subject);
		object.setBody(body);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<Boolean> argument) throws IOException {
		SendEmail object = (SendEmail) argument;

		// Get fields
		long loginId = object.getLoginId();
		String sender = object.getSender();
		String recipient = object.getRecipient();
		String subject = object.getSubject();
		String body = object.getBody();

		// Write fields
		writer.writeLong(loginId);
		writer.writeString(sender, true);
		writer.writeString(recipient, true);
		writer.writeString(subject, true);
		writer.writeString(body, true);
	}

	@Override
	public IExecutableCommandResponse<Boolean> newResponse() {
		return new SendEmailResponse();
	}
}
